package com.oss.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.oss.model.*;
import com.oss.view.InvokeResult;

import java.util.Date;

public class OrderService {
    private static final Users usersDao = new Users().dao();
    private static final Employee employeeDao = new Employee().dao();
    private static final Orders ordersDao = new Orders().dao();
    private static final Dealer dealerDao = new Dealer().dao();
    private static final Series seriesDao = new Series().dao();


    static Log log = Log.getLog(OrderService.class);


    /**
     * 判断订单是否异常:是否异常：1-是；0-否
     */
    public InvokeResult isAbnormal(String employeeId, String memberCode) {
        Employee employee = employeeDao.findById(employeeId);
        if (employee == null) {
            return InvokeResult.failure("该账号已失效");
        }
        Users users = usersDao.findFirst("SELECT * FROM users WHERE member_code=?", memberCode);
        if (users == null) {
            return InvokeResult.failure("用户不存在");
        }

        Integer dealerId = employee.getDealerId();
        Integer regDealerId = users.getRegDealerId();

        Integer isAbnormal = 0;
        if (!dealerId.equals(regDealerId)) {
            isAbnormal = 1;
        }

        return InvokeResult.success(isAbnormal);
    }


    /**
     * 分页查询店员核销订单
     */
    public InvokeResult queryOrderListByEmployee(Integer pageNumber, Integer pageSize, String employeeId, String start, String end) {

        String sqlExceptSelect = " FROM `orders` WHERE employee_id= " + employeeId;

        if (Validator.isNotEmpty(start) && Validator.isNotEmpty(end)) {
            sqlExceptSelect = sqlExceptSelect + " AND DATE_FORMAT(create_time,'%Y-%m-%d') BETWEEN '" + start + "' AND '" + end + "' ";
        }

        System.out.println(sqlExceptSelect);

        Page<Orders> paginate = ordersDao.paginate(pageNumber, pageSize, "SELECT * ", sqlExceptSelect + " ORDER BY id DESC");

        return InvokeResult.success(paginate);
    }


    /**
     * 核销订单
     *
     * @param memberCode
     * @param employeeId
     * @param seriesId
     * @return
     */
    public InvokeResult addOrder(String memberCode, String employeeId, Integer seriesId) {
        Users users = usersDao.findFirst("SELECT * FROM users WHERE member_code=?", memberCode);
        if (users == null) {
            return InvokeResult.failure("用户不存在");
        }

        Employee employee = employeeDao.findById(employeeId);
        Integer dealerId = employee.getDealerId();
        Dealer dealer = dealerDao.findById(dealerId);

        String seriesIds = dealer.getSeriesId();


        Series series = seriesDao.findById(seriesId);

        Orders orders = new Orders();
        orders.setUsersId(users.getId());
        orders.setSeriesId(seriesId);
        orders.setEmployeeId(Integer.valueOf(employeeId));
        orders.setDealerId(dealerId);

        orders.setUsersName(users.getName());
        orders.setUsersPhone(users.getMobile());
        orders.setUsersMemberCode(users.getMemberCode());
        orders.setSeriesName(series.getName());
        orders.setEmployeeName(employee.getAccount());
        orders.setDealerName(dealer.getName());

        String orderNumber = users.getRegDealerId() + users.getMemberCode() + DateUtil.format(new Date(), "yyyyMMdd") + employee.getDealerId() + seriesId + RandomUtil.randomNumbers(4);
        orders.setOrderNumber(orderNumber);

        Integer isAbnormal = 0;//是否异常：1-是；0-否
        if (!users.getRegDealerId().equals(dealerId)) {
            isAbnormal = 1;
            users.setDealerId(dealerId);
        }

        if (!seriesIds.contains(seriesId.toString())) {
            isAbnormal = 1;
        }

        orders.setIsAbnormal(isAbnormal);
        orders.setCreateTime(new Date());
        orders.save();


        //会员等级升级：1-初级会员;2-高级会员
        users.setLevelId(2);
        users.update();

        return InvokeResult.success("核销成功", orders);
    }


}
